package com.itstu.gulimall.seckill.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.fastjson.JSON;
import com.itstu.common.exception.BizCodeEnume;
import com.itstu.common.utils.R;
import org.springframework.context.annotation.Configuration;

/**
 * @author shhjiang_001
 * @create 2020-06-07 14:43
 */
@Configuration
public class SeckillSentinelConfig {
    public SeckillSentinelConfig(){
        WebCallbackManager.setUrlBlockHandler((httpServletRequest, httpServletResponse, e) -> {
            R error = R.error(BizCodeEnume.TOO_MANEY_REQUEST_EXCEPTION.getCode(), BizCodeEnume.TOO_MANEY_REQUEST_EXCEPTION.getMsg());
            httpServletResponse.setCharacterEncoding("UTF-8");
            httpServletResponse.setContentType("application/json");
            httpServletResponse.getWriter().write(JSON.toJSONString(error));
        });
    }
}
